#   Copyright (c)  2021 FromVB2Python
#   FromVB2Python is licensed under Mulan PSL v2.
#   You can use this software according to the terms and conditions of the Mulan PSL v2. 
#   You may obtain a copy of Mulan PSL v2 at:
#            http://license.coscl.org.cn/MulanPSL2 
#   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
#   See the Mulan PSL v2 for more details.

# 057-选考A-环形数据排序-202011-15温州选考模拟
import tkinter as tk
import math
import random

def c(x,y,r,cav,fl=''):
    '''根据圆心x,y和半径r画圆'''
    x1, y1 = x-r, y-r
    x2, y2 = x+r, y+r
    cav.create_oval(x1,y1,x2,y2,fill=fl)
    cav.update()

def init():
    '''清空左右侧数字，画好基本图形'''
    global lst
    cav1.delete('all')  # 清空左侧图布
    cav2.delete('all')  # 清空右侧图布
    x,y,r=150,150,120
    c(150,150,120,cav1)  # 画左侧大圆
    c(150,150,120,cav2)  # 画右侧大圆
    lst, g = [], math.pi/180
    for i in range(-4,6):  # 生成各小圆圆心坐标并存放在lst中
        lst.append([x+r*math.cos(g*i*36),x+r*math.sin(g*i*36)])
    for i in lst:  # 画左右各10个小圆
        c(i[0],i[1],28,cav1, fl = 'white')
        c(i[0],i[1],28,cav2, fl = 'white')


def generate(rndFlag=True):
    '''初始化数据列表，填好左侧数字'''
    global lst,nlst
    init()  # 清空与初始化图形
    if not rndFlag:
        nlst = [1,11,19,18,0,16,8,9,17,4]
    else:
        nlst = [random.randint(1,99) for i in range(10)]
    for i in range(10):  # 把nlst数组数据填到左侧各小圆中
        cav1.create_text(lst[i][0],lst[i][1],text=str(nlst[i]))
    btn2['state']='normal'
def change():
    '''执行命令按钮动作,排序并填好右侧数字'''
    global lst,nlst
    mn = nlst.index(min(nlst))
    nlst.sort()
    nlst = nlst[mn::-1] + nlst[-1:mn:-1]
    for i in range(10):  # 把nlst数组数据填到右侧各小圆中
        cav2.create_text(lst[i][0],lst[i][1],text=str(nlst[i]))
    btn2['state']='disable'
    
# 创建窗体
root = tk.Tk()
root.title('环形数据排序')
root.resizable(0, 0)
# 创建组件(左右画布及两个按钮)
cav1 = tk.Canvas(root, width=300, height=300, bg = 'lightblue')
cav2 = tk.Canvas(root, width=300, height=300, bg = 'lightgreen')
btn1 = tk.Button(text='产生数组',command=generate)
btn2 = tk.Button(text='进行排序',command=change)
# 进行组件布局
cav1.grid(row=0,column=0)
cav2.grid(row=0,column=1)
btn1.grid(row=1,column=0)
btn2.grid(row=1,column=1)
# 用给定数组填好左侧环形数据
generate(rndFlag=False)
# 接收窗体事件
root.mainloop()
